package com.fingard.dsp.bank.directbank.cscb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqPayRecDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqQrySglBatPayDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 长沙银行-单笔支付
 * @author alan
 * @date 2020-05-18
 * @version 1.0
 */
public class CSCB01SinglePay extends CSCB01Base implements ISinglePay {

    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        try {
            //赋值通用参数
            CSCB01Header bankHeader = new CSCB01Header();
            bankHeader.trCode = "300001";
            bankHeader.reqNo = p_req1908.reqHeader.reqSeqID;
            bankHeader.fileFlag = "0";//文件标识 0-报文 1-文件
            writeCommonHeader(xmlWriter, bankHeader);
            XElement body = new XElement("body","");
            body.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));//付款账号
            body.addChild(new XElement("pay_cur_code", convertCurToBank(p_req1908.singleDetail.payCur)));//付款币种
            body.addChild(new XElement("pay_acname", p_req1908.singleDetail.payName));//付款户名
            body.addChild(new XElement("as_flag", "0"));//账簿标志 0-不需要 1-需要
            body.addChild(new XElement("as_acno", ""));//账簿号
            body.addChild(new XElement("as_acname", ""));//账簿名称
            body.addChild(new XElement("pay_accaddr", ""));//付款地址
            body.addChild(new XElement("cert_type", ""));//凭证种类
            body.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));//凭证号(企业账务流水号)
            body.addChild(new XElement("rcv_acno", p_req1908.singleDetail.recAct));//收款账号
            body.addChild(new XElement("rcv_cur_code", convertCurToBank(p_req1908.singleDetail.recCur)));//收款币种
            body.addChild(new XElement("rcv_acname", p_req1908.singleDetail.recName));//收款户名
            body.addChild(new XElement("rcv_accaddr", ""));//收款人地址
            body.addChild(new XElement("rcv_bank_no", p_req1908.singleDetail.cnapsCode));//收款行号，收款方账号联行行号
            body.addChild(new XElement("rcv_bank_name", p_req1908.singleDetail.cnapsName));//收款行名
            body.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));//交易金额
            body.addChild(new XElement("bank_flag", "0".equals(p_req1908.singleDetail.sameBnk) ? "1" : "0"));//银行标志 0：本行 1：他行
            body.addChild(new XElement("urgency_flag", p_req1908.singleDetail.paySpd));//加急标志 0：普通 1：加急
            body.addChild(new XElement("area_flag", p_req1908.singleDetail.sameCity));//同城异地标志 0：同城 1：异地
            body.addChild(new XElement("bank_kind", ""));//银行类别
            body.addChild(new XElement("purpose", p_req1908.singleDetail.purpose));//用途  企业转账摘要信息
            body.addChild(new XElement("postscript", ""));//附言
            body.addChild(new XElement("booking_flag", "0"));//预约标志 0－是实时交易 1－是预约交易
            body.addChild(new XElement("booking_date", ""));//预约日期
            body.addChild(new XElement("booking_time", ""));//预约时间
            body.addChild(new XElement("saverecvinfo_flag", ""));//是否保存收款方信息
            body.addChild(new XElement("mobiles", ""));//收款方手机号码
            nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());
            xmlWriter.writeXElement(body);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_req1908.reqHeader.transType, p_req1908.reqHeader.commandCode);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpsuccFlag = tmpBkReader.getFirstText("/ap/head/succ_flag");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/head/ext_info");
                    tmpRetAtsDetail.respCode = "0";
                    tmpRetAtsDetail.respInfo = StringHelper.joinNotEmpty(";", tmpRetInfo, tmpRetRxtInfo);
                    tmpRetAtsDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    if(!"0".equals(tmpsuccFlag) && !"1".equals(tmpsuccFlag) && !"8".equals(tmpsuccFlag)){
                        tmpsuccFlag="other";
                    }
                    tmpRetAtsDetail.transState = tmpsuccFlag;
                    tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    //1944 交易状态查询
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //赋值通用参数
            CSCB01Header bankHeader = new CSCB01Header();
            bankHeader.trCode = "200205";
            bankHeader.reqNo = p_req1944.reqHeader.transSeqID;
            bankHeader.fileFlag = "0";//文件标识 0-报文 1-文件
            writeCommonHeader(xmlWriter, bankHeader);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("cert_no", p_req1944.askNumber));//企业erp[账务]流水号
            tmpXElement.addChild(new XElement("req_no", p_req1944.askNumber));//企业erp［请求］流水号
            tmpXElement.addChild(new XElement("tr_acdt", Format.Date8Format.format(p_req1944.getBgnDateTime())));
            tmpXElement.addChild(new XElement("cms_corp_no", ""));//客户号
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_req1944.reqHeader.transType, p_req1944.reqHeader.commandCode);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/body/ext_info");
                    String succ_flag = tmpBkReader.getFirstText("/ap/head/succ_flag");
                    if ("0000".equals(tmpRespCode) && "0".equals(succ_flag)) {
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = tmpBkReader.getFirstText("/ap/body/error_info");
                        tmpRetAtsDetail.reqSeqID = tmpBkReader.getFirstText("/ap/body/req_no");
                        tmpRetAtsDetail.transState = tmpBkReader.getFirstText("/ap/body/stat");//支付状态码
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;//支付信息码
                    } else if("2100".equals(tmpRespCode) && "2".equals(succ_flag)){
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = tmpBkReader.getFirstText("/ap/body/error_info");
                        tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                        tmpRetAtsDetail.transState = tmpRespCode+"+"+succ_flag;//支付状态码
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;//支付信息码
                    }else {
                        tmpRetAtsDetail.payInfoCode = "-1";
                        tmpRetAtsDetail.respInfo = StringHelper.joinNotEmpty(";", tmpRetInfo, tmpRetRxtInfo);
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        //返回ATS的对象
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1908BAT);
        retAts.reqBatchNO = p_req1908BAT.reqBatchNO;
        retAts.batRespCode = "0";
        retAts.batTransState = "";
        retAts.batPayInfoCode = "";
        while (p_req1908BAT.readRecord()) {
            ReqPayRecDetail tmpDetail = p_req1908BAT.readedDetail;
            ReqSingle req1908 = new ReqSingle(p_req1908BAT.reqHeader);
            req1908.singleDetail = tmpDetail;
            RetAtsSingle eachRet = d_1908_SglPay(req1908);

            retAts.writeRetDetail(eachRet.retDetail);
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);
        retAts.reqBatchNO = p_req1944BAT.reqBatchNO;
        try {
            retAts.batRespCode = "0";
            while (p_req1944BAT.readRecord()) {
                ReqQrySglBatPayDetail tmpDetail = p_req1944BAT.readedDetail;
                ReqQrySingle req1944 = new ReqQrySingle(tmpDetail, p_req1944BAT.reqHeader);
                RetAtsSingle resp1944 = d_1944_QrySglPay(req1944);
                retAts.writeRetDetail(resp1944.retDetail);
            }
        } catch (Exception ex) {
            WriteBankLogLn(ex);
            retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);
            retAts.batRespCode = "-1";
            retAts.batRespInfo = ex.getMessage();
        }

        retAts.writeAfterDetail();
        return retAts;
    }
}
